let requestUrl = null;

function addHandler() {
	requestUrl = "/school_master/insert";

	$("#master_dialog").dialog("open");
}

function editHandler() {
	let data = $("#master_list").datagrid("getSelected");

	if (data) {
		requestUrl = "/school_master/updateById";

		$("#id").val(data.id);
		$("#name").textbox("setValue", data.name);
		$("#schoolId").combobox("setValue", data.schoolId);

		$("#master_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

function imageHandler() {
	let rowData = $("#master_list").datagrid("getSelected");

	if (rowData) {
		$("#master_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

$(document).ready(function() {

	$("#school_id").combobox({
		url: "/school/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		prompt: "-选择门派-",
		formatter: function(row) {
			return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#search").linkbutton({
		text: "搜索",
		iconCls: "icon-search"
	}).click(function() {
		let schoolId = $("#school_id").combobox("getValue");

		$("#master_list").datagrid("reload", {
			schoolId: schoolId
		});
	});

	$("#clear").linkbutton({
		text: "清空",
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#name").textbox({
		width: 150,
		required: true
	});

	fetchSchoolId();

	$("#master_dialog").dialog({
		title: "门派师傅信息",
		width: 500,
		modal: true,
		closed: true,
		closable: false,
		draggable: false,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#master_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#master_dialog").dialog("close");
						$("#master_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#master_dialog").dialog("close");
				$("#master_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#master_list", "/school_master/upload");

	fetchUploadDialog("/school_master/updateImageById", function () {
		$("#master_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#master_list").datagrid({
		url: "/school_master/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "name", title: "师傅名字", align: "center", width: 100},
			{field: "image", title: "师傅图片", align: "center", width: 100
				, formatter: function(value) {
					return "<img alt='暂无图片' height='" + size + "' src='" + value + "' />";
				}
			},
			{field: "schoolId", title: "所属门派", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/school/selectById", {
							id: value
						}, function(resp) {
							let data = resp.data;

							$("#school_" + rowIndex).attr("src", data.image)
								.attr("title", data.name);
						}, error);

						return "<img height='" + size + "' id='school_" + rowIndex + "' />";
					} else {
						return "<span>/</span>";
					}
				}
			}
		]]
	});

});